/*
 * Copyright (C) 2010-2011 Graz University of Technology, Austria. All Rights reserved. 
 *
 * Contact: Simone Kopeinik <simone.kopeinik@tugraz.at>
 * 	   Graz University of Technology, Knowledge Management Institute, Brückenkopfgasse 1/6, 8020 Graz 	
 * 	   <http://www.kmi.tugraz.at/>
 * 
 * This software is part of the TARGET platform developed by the
 * TARGET EC-funded project www.reachyourtarget.org
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The GNU General Public License is available at <http://www.gnu.org/licenses/>.
 *
 */
package at.tugraz.kmi.css.analyzer.gapvis.koichi;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.List;

import javax.swing.Icon;

import at.tugraz.kmi.css.analyzer.UserCompetence;
import at.tugraz.kmi.css.analyzer.gapvis.GraphEdge;
import edu.uci.ics.jung.visualization.VisualizationViewer;

class CompetenceVertexIcon implements Icon {
	private final VisualizationViewer<UserCompetence, GraphEdge>	viewer;
	private final UserCompetence									competence;

	public CompetenceVertexIcon(
			VisualizationViewer<UserCompetence, GraphEdge> viewer,
			UserCompetence competence) {
		this.viewer = viewer;
		this.competence = competence;
	}

	@Override
	public int getIconHeight() {
		return 24;
	}

	@Override
	public int getIconWidth() {
		return 24;
	}

	@Override
	public void paintIcon(Component c, Graphics g, int x, int y) {
		Graphics2D graphics = (Graphics2D) g.create();

		Font font = graphics.getFont();
		graphics.setFont(new Font(font.getName(), font.getStyle(), font
				.getSize() - 2));
		FontMetrics metrics = graphics.getFontMetrics();

		List<String> lines = competence.getCommentLines();
		if (lines.size() > 0) {
			int lineH = metrics.getAscent();
			int lineY = y + 12;
			int lineX = x + 24 + 8;

			// Draw the string of a comment.
			if (competence.isCommentVisible()) {
				graphics.setColor(Color.GRAY);
				for (int i = 0; i < lines.size(); i++) {
					graphics.drawString(lines.get(i), lineX, lineY);
					lineY += lineH;
				}
			}

			// // Draw the line from a competence vertex to a comment string.
			// graphics.setColor(Color.BLACK);
			// graphics.drawLine(x+12, y+12, lineX, y+24+10);
		}

		// Paint the icon of a vertex.
		if (viewer.getPickedVertexState().isPicked(competence)) {
			graphics.setColor(Color.YELLOW);
		} else {
			if (competence.getProficiency() >= 80) {
				graphics.setColor(Color.RED);
			} else if (competence.getProficiency() >= 60) {
				graphics.setColor(Color.ORANGE);
			} else if (competence.getProficiency() >= 40) {
				graphics.setColor(Color.GREEN);
			} else if (competence.getProficiency() >= 20) {
				graphics.setColor(Color.CYAN);
				// } else if(competence.getProficiency() >= 10) {
				// graphics.setColor(Color.BLUE.brighter().brighter());
			} else {
				graphics.setColor(Color.LIGHT_GRAY);
			}
		}
		graphics.fillOval(x, y, 24, 24);
		graphics.setColor(Color.BLACK);

		graphics.setFont(font);
		metrics = graphics.getFontMetrics();
		int width = metrics.stringWidth("" + competence.getProficiency());
		int height = metrics.getAscent();
		graphics.drawString("" + competence.getProficiency(), x + 12 - width
				/ 2, y + 12 + height / 2);
		if (viewer.getPickedVertexState().isPicked(competence)) {
			graphics.setColor(Color.RED);
			graphics.setStroke(new BasicStroke(2.5f));
		} else {
			graphics.setColor(Color.BLACK);
			graphics.setStroke(new BasicStroke(1f));
		}
		graphics.drawOval(x, y, 24, 24);

		graphics.dispose();
	}
}
